//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __CEVENTMANAGER_H__
#define __CEVENTMANAGER_H__

#define _NO_CDEVICEEVENTMANAGER_CLASSOBJECT_
#define _NO_INCLIST
#include <elasys_server.h>
#define __NO_LINKNODE_CONSTRUCTOR
#include <linknode.h>

#include "_CEventManager.h"

#define EVENT_HASH_LENGTH 128
#define EVENT_HASH_INDEX(eventnum) (eventnum % EVENT_HASH_LENGTH)

//typedef struct EveHandler {
//    Int32 nOrder;
//    IEventDelegate *pEveDelegate;
//    struct EveHandler *pNextHandler;
//} EveHandler, *PEveHandler;

//typedef struct EventInfo : public DLinkNode {
//    UInt32 uEventNum;
//    PEveHandler pHandlerList;
//} EventInfo, *PEventInfo;

typedef struct EventDelegate : public DLinkNode {
    Int32 uEventNum;
    IEventDelegate *pEveDelegate;
} EventDelegate, *PEventDelegate;

//typedef struct EventNode : public DLinkNode {
//    UInt32 uEventNum;
//    union {
//        struct {
//            UInt32 uNum;
//            EmIsrParameter isrpm;
//        } ipm;
//        MemoryBuf eventData;
//    }um;
//    _ELASTOS Boolean bIsrParameter;
//}EventNode, *PEventNode;

typedef struct EventNodeBase : public DLinkNode {
    Int32 uEventNum;
    Boolean bIsrParameter;
}EventNodeBase, *PEventNodeBase;

typedef struct EventNode : EventNodeBase {
    MemoryBuf* pEventData;
}EventNode, *PEventNode;

typedef struct EventIsrNode : EventNodeBase {
    Int32 uNum;
    EmIsrParameter isrpm;
}EventIsrNode, *PEventIsrNode;


typedef struct EventIsrBlockNode : public DLinkNode {
    EventIsrNode eventIsrNode;
}EventIsrBlockNode, *PEventIsrBlockNode;

class CEventManager : public _CEventManager
{
public:
    CARAPI SendEvent(
        /* [in] */ Int32 eventNum,
        /* [in] */ const MemoryBuf & eventData);

    CARAPI PostEvent(
        /* [in] */ Int32 eventNum,
        /* [in] */ Int32 priority,
        /* [in] */ const MemoryBuf & eventData);

    CARAPI AddEventHandler(
        /* [in] */ Int32 eventNum,
        /* [in] */ Int32 nOrder,
        /* [in] */ IEventDelegate * pDelegate);

    CARAPI RemoveEventHandler(
        /* [in] */ Int32 eventNum,
        /* [in] */ IEventDelegate * pDelegate);

    ECode Initialize();

    ~CEventManager();

private:
    // TODO: Add your private member variables here.
};

#endif // __CEVENTMANAGER_H__
